package com.powermgr.boringtest.task.impl;

import com.powermgr.boringtest.bean.PrData;
import com.powermgr.boringtest.task.PrTask;
import com.powermgr.boringtest.utils.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import static com.powermgr.boringtest.utils.ConfigUtils.*;

/**
 * DownloadVersionTask
 *
 * @author qiuxiangdong
 * @date 2023/9/23 10:26
 */
@Log4j2
public class DownloadNewPrTask implements PrTask<PrData> {
    @Override
    public boolean execute(PrData prData) {
        log.info("SN- " + prData.getSn_id() + ": " + "DownloadNewPrTask START");
        log.info("SN- " + prData.getSn_id() + ": " + "1.下载版本");
        if (!versionDownload(prData.getVersion_url(), prData.getVersion_name(),prData.getSn_id())){
            EmailUtils.SendEmail(prData, VERSION_DOWNLOAD_FALSE);
            return false;
        }
        if (!FileZipUtils.unTarGzVersion(prData.getVersion_name(), prData.getSn_id())){
            EmailUtils.SendEmail(prData, UN_TAR_GZ_FALSE);
            return false;
        }
        if (!prData.getXts_url().isEmpty()){
            log.info("SN- " + prData.getSn_id() + ": " + "2.下载XTS用例");
            xtsDownload(prData.getXts_url(),prData.getXts_name(),prData.getSn_id());
            if (TypeUtils.IsXTS(prData.getType())){
                log.info("SN- " + prData.getSn_id() + ": " + "3.解压XTS用例");
                if (FileZipUtils.unTarGzXts(prData.getXts_name(), prData.getSn_id())){
                    EmailUtils.SendEmail(prData, UN_TAR_GZ_FALSE);
                    return false;
                }
            }
        }
        if (!prData.getTdd_url().isEmpty()){
            log.info("SN- " + prData.getSn_id() + ": " + "2.下载TDD用例");
            tddDownload(prData.getTdd_url(), prData.getTdd_name(),prData.getSn_id());
            if (TypeUtils.IsTDD(prData.getType())){
                log.info("SN- " + prData.getSn_id() + ": " + "3.解压TDD用例");
                if (FileZipUtils.unTarGzTdd(prData.getTdd_name(), prData.getSn_id())){
                    EmailUtils.SendEmail(prData, UN_TAR_GZ_FALSE);
                    return false;
                }
            }
        }
        log.info("SN- " + prData.getSn_id() + ": " + "DownloadNewPrTask END");
        return true;
    }
    private boolean versionDownload(String downloadUrl, String version_name, String sn) {
        return download(downloadUrl, version_name, ConfigUtils.SN + File.separator + sn +  File.separator + "pr" + File.separator + "version");
    }
    private boolean tddDownload(String downloadUrl, String tdd_name, String sn) {
        return download(downloadUrl, tdd_name, ConfigUtils.SN + File.separator + sn +  File.separator + "pr" + File.separator + "tdd");
    }
    private boolean xtsDownload(String downloadUrl, String xts_name, String sn) {
        return download(downloadUrl, xts_name, ConfigUtils.SN + File.separator + sn +  File.separator + "pr" + File.separator + "xts");
    }
    private boolean download(String downloadUrl, String name, String path) {
        log.info("*开始下载: " + downloadUrl);
        log.info("*开始下载: " + path + File.separator + name);
        try {
            long bytesum = 0;
            int byteread = 0;
            URL url = new URL(downloadUrl);
            URLConnection conn = url.openConnection();
            long contentLength = (conn.getContentLengthLong() / 1024) / 1024;
            log.info("*下载文件总大小: " + contentLength + "Mb");
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream(path + File.separator + name);
            byte[] buffer = new byte[1024];
            long len = 0;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
                long x = (bytesum / 1024) / 1024;
                if (len != x) {
                    if (x % 100 == 0) {
                        log.info("文件大小: " + contentLength + "Mb，已下载：" + x + "Mb");
                    }
                }
                len = x;
            }
            log.info("文件大小: " + contentLength + "Mb，已下载：" + contentLength + "Mb");
            return true;
        } catch (IOException e) {
            log.error("*download 错误 重新下载",e);
            return false;
        }
    }
}
